/* Copyright (c) 2008, 2009, 2010, 2011 Damian Kmiecik
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
   * Neither the name of the copyright holders nor the names of
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE. */

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <avr/wdt.h>
#include "hardware.h"
#include "main.h"
#include "timer.h"
#include "usart.h"
#include "ethernet.h"
#include "flash.h"

struct usart_receive_s usart_receive;

#define CPU				0xFA
#define FLASH			0xFB
#define CONF			0xFC
#define EXIT			0xFD
#define ESCAPE			0xF4
#define FLASH_WRITE		0xFE
#define FLASH_READ		0xF2
#define EEPROM_WRITE	0xC0
#define EEPROM_READ     0xC1

#define CONF_VAL	0xFF

inline void flashReadRoutine(void)
{
	while (usart_receive.data_size < 4);
	uint32_t size = 0;
	size |= ((uint32_t)usartBuffer[0])<<24;
	size |= ((uint32_t)usartBuffer[1])<<16;
	size |= ((uint32_t)usartBuffer[2])<<8;
	size |= ((uint32_t)usartBuffer[3]);
	uint32_t addr;
	for (addr = 0; addr < size; addr++)
	{
		usartPutc(flashReadByte(addr));
	}
}

inline void flashWriteRoutine(void)
{
	flashErase();
	uint32_t addr = 0;
	while (addr < FLASH_SIZE)
	{
		while (usart_receive.data_size < 100);
		usart_receive.data_size = 0;
		for (uint8_t i = 0; i < 100; i++)
		{
			flashWriteByte(addr, usartBuffer[i]);
			addr++;
		}
		usartPutc(ESCAPE);
	}
}

inline void eepromWriteRoutine(void)
{
	uint16_t addr = 0;
	while (addr < FLASH_SIZE)
	{
		while (usart_receive.data_size < 100);
		usart_receive.data_size = 0;
		for (uint8_t i = 0; i < 100; i++)
		{
			uint8_t *ptr = addr;
			eeprom_write_byte(ptr, usartBuffer[i]);
			addr++;
		}
		usartPutc(ESCAPE);
	}
}

inline void eepromReadRoutine(void)
{
	while (usart_receive.data_size < 2);
	uint16_t size = 0;
	size |= ((uint16_t)usartBuffer[0])<<8;
	size |= ((uint16_t)usartBuffer[1]);
	uint16_t addr;
	for (addr = 0; addr < size; addr++)
	{
		uint8_t *ptr = addr;
		usartPutc(eeprom_read_byte(ptr));
	}
}

inline void usartRoutine(void)
{
	wdt_disable();
	usart_receive.data_size = 0;
	usart_receive.enabled = TRUE;
	sei();
	while (1)
	{
		while (usart_receive.data_size == 0);
		usart_receive.data_size = 0;
		if (usartBuffer[0] == CPU)
			usartPutc(32);
		if (usartBuffer[0] == FLASH)
			usartPutc(FLASH_SIZE*8/1024/1024);
		if (usartBuffer[0] == CONF)
			usartPutc(CONF_VAL);
		if (usartBuffer[0] == FLASH_WRITE)
			flashWriteRoutine();
		if (usartBuffer[0] == FLASH_READ)
			flashReadRoutine();
		if (usartBuffer[0] == EEPROM_WRITE)
			eepromWriteRoutine();
		if (usartBuffer[0] == EEPROM_READ)
			eepromReadRoutine();
		if (usartBuffer[0] == EXIT)
			break;
	}
	usart_receive.enabled = FALSE;
	wdt_enable(WDTO_1S);
}

void usartProcess(void)
{
	if ((usartGetc() == 0xEB) && (usartGetc() == 0x99))
	{
		usartPutc(0x9A);
		usartRoutine();
	}
}

ISR(USART_RXC_vect)
{
	if (usart_receive.enabled == TRUE)
	{
		char ch = UDR;
		if (usart_receive.data_size < USART_RECEIVE_BUFFER)
			usartBuffer[usart_receive.data_size++] = ch;
	}
}

uint8_t usartGetc(void)
{
	uint8_t a = 0;
	while (!(UCRA & _BV(RXC)) && (a++ < 100))
		_delay_ms(1);
	if (a >= 100)
		return 0;
	return UDR;
}
